'use strict';

const Controller = require('egg').Controller;
class ArticleListController extends Controller {
    // 获取文章列表

    async commentLists() {
        const { ctx } = this;
        const { pageCode, pageSize, name, email,state,inner,htmlInner,message,date,father } = ctx.request.body;
        // console.log(user,'11')
        // console.log(status, 'kkkka')

        let commentLists = await this.app.mysql.select('commentLists');
        // console.log(lists.length)
        let newLists = commentLists;
        // console.log(num,'ii')
        if (name) {
            newLists = newLists.filter(v => v.name.includes(name))
        }
        if (email) {
            newLists = newLists.filter(v => v.email.includes(email))
        }
        if (state) {
            newLists = newLists.filter(v => v.state==state)
        }
        // console.log(num, 'kk')
        let total = newLists.length;
        newLists = newLists.slice((pageCode - 1) * pageSize, pageCode * pageSize)
        // console.log(newLists, 'ff')

        if (newLists.length) {
            ctx.body = {
                code: 200,
                total,
                results: newLists
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '获取数据失败'
            }
        }
    }

    // 删除文章列表
    async deleteCommentLists() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('commentLists', { id });
        if (result) {
            const list = await this.app.mysql.select('commentLists');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list
            }
        } else {
            ctx.body = {
                code: 200,
                msg: '删除失败',
                list
            }
        }

    }




    // 多选删除
    async commentDel() {
        const { ctx } = this;
        const { multipleSelection, pageCode, pageSize } = ctx.request.body;
        console.log(multipleSelection, 'lll')
        const chooseLists = multipleSelection.map(v => {
            return this.app.mysql.delete('commentLists', { id: v.id })
        });
        console.log(chooseLists, 'jjj')
        if (chooseLists) {
            const list = await this.app.mysql.select('commentLists');
            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '删除成功',
                    list: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
                    total: list.length
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '删除失败',
                }
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '多选删除错误',
            }
        }
    }

    //批量删除
    // async moreDel() {
    //     const { ctx } = this;
    //     const { id } = ctx.request.body
    //     console.log(id,'====')
    //     let sql = `DELETE FROM commentLists WHERE id in (${id})`

    //     try {
    //         const results = await this.app.mysql.query(sql)
    //         ctx.body = {
    //             code: 200,
    //             msg: '删除成功',
    //         }
    //     } catch (err) {
    //         ctx.body = {
    //             code: 505,
    //             msg: '服务器繁忙，请稍后重试',
    //             err,
    //         }
    //     }
    // }

    // 通过
    async through() {
        const { ctx } = this;
        const { id } = ctx.request.body;
        // const chooseLists = multipleSelection.map(v => {
        //     return this.app.mysql.update('articleLists', { id: v.id, state: 'false' })

        // });
        const row = {
            state: 'true'
        };

        const options = {
            where: {
                id
            }
        };
        const result = await this.app.mysql.update('commentLists', row, options); // 更新 posts 表中的记录

        if (result) {
            const list = await this.app.mysql.select('commentLists');
            ctx.body = {
                code: 200,
                msg: '通过成功',
                list,
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '通过失败',
            }
        }



    }

    // 拒绝
    async refused() {
        const { ctx } = this;
        const { id } = ctx.request.body;
        // const chooseLists = multipleSelection.map(v => {
        //     return this.app.mysql.update('articleLists', { id: v.id, state: 'false' })

        // });
        const row = {
            state:'false'
        };

        const options = {
            where: {
                id
            }
        };
        const result = await this.app.mysql.update('commentLists', row, options); // 更新 posts 表中的记录
          
         if (result) {
             const list = await this.app.mysql.select('commentLists');
                ctx.body = {
                    code: 200,
                    msg: '拒绝成功',
                    list,
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '拒绝失败',
                }
            }
       
    }


}

module.exports = ArticleListController;
